package com.wind.util;

import com.baomidou.mybatisplus.enums.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Scanner;

public class Generator {


    public static void main(String[] args) {
        String packageName = "com.wind";
        String path = "E:\\windTmpCode\\";
        System.out.println("输入表名：");
        Scanner input = new Scanner(System.in);
        String tableName = input.next();
        boolean serviceNameStartWithI = true;//auth -> UserService, 设置成true: auth -> IUserService
        generateByTables(path, serviceNameStartWithI, packageName, "xiao", "heart", tableName);

        System.out.println("completed...");
    }

    /**
     * @param serviceNameStartWithI
     * @param packageName           包名
     * @param author                作者
     * @param database              数据库名
     * @param tableNames            表名
     */
    private static void generateByTables(String path, boolean serviceNameStartWithI, String packageName, String author, String database, String... tableNames) {
        GlobalConfig config = new GlobalConfig();
        String dbUrl = "jdbc:mysql://49.234.218.252:3306/" + database
                + "?verifyServerCertificate=false&useSSL=false&autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig
                .setUrl(dbUrl)
                .setUsername("heart")
                .setPassword("ny8pFRCdCsm4STmK")
                .setDriverName("com.mysql.jdbc.Driver");
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                .setEntityLombokModel(true)
                .setRestControllerStyle(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组

        config.setActiveRecord(false)
                .setIdType(IdType.AUTO)
                .setAuthor(author)
                .setOutputDir(path)//生成文件路径就是你实际项目的路径
                .setFileOverride(true)
                .setBaseResultMap(true)//XML ResultMap
                .setEnableCache(false);//二级缓存
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");//%s为实体类名称后加services  还有其他文件得名称都可更改，这里我没有找到Entity修改的  待确定？
            //.setIdType(IdType.AUTO);//主键生成策略
        }
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(new PackageConfig()//设置包名
                        .setParent(packageName)
                        .setController("controller")
                        .setEntity("entity")
                        .setMapper("dao")
                        .setService("service")
                        .setServiceImpl("service.impl")
                        .setXml("dao.mapper")
                ).execute();
    }
}
